package com.juku2024.juku

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.juku2024.juku.adapter.DebLockingAdapter
import com.juku2024.juku.base.BaseActivity
import com.juku2024.juku.db.entity.UnlockMoreModel
import com.juku2024.juku.model.Genre
import com.juku2024.juku.network.retrofit.OnRetrofitResultListener
import com.juku2024.juku.viewmodel.UnlockViewModel
import com.juku2024.juku.widget.HeaderView
import com.juku2024.juku.widget.LoadingView

class UnlockMoreActivity : BaseActivity() {

    private lateinit var headView: HeaderView
    private lateinit var recycler: RecyclerView
    private lateinit var groupNotLogin: View
    private lateinit var loadingView: LoadingView

    private val viewModel by lazy { getViewModel(UnlockViewModel::class.java) }

    override fun getLayoutResId(): Int {
        return R.layout.activity_deblocking_more
    }

    override fun initView() {
        headView = findViewById(R.id.headView_deblocking_more)
        recycler = findViewById(R.id.recycler_deblocking_more)
        groupNotLogin = findViewById(R.id.groupNotLogin)
        loadingView = findViewById(R.id.loadingView_deblocking_more)


        headView.setTitle("解锁更多")
        recycler.layoutManager = LinearLayoutManager(this)
        recycler.setHasFixedSize(true)
    }

    override fun init(savedInstanceState: Bundle?) {
        groupNotLogin.visibility = View.GONE
        loadingView.showLoading()
        doHttpUnlockMore()
    }



    private fun doHttpUnlockMore() {
        viewModel.getUnlockList(object: OnRetrofitResultListener<UnlockMoreModel> {
            override fun onSuccess(t: UnlockMoreModel) {
                if (isFinishing || isDestroyed) return
                loadingView.hideLoading()
                if (t.data.isNullOrEmpty()) {
                    loadingView.showEmptyStatus("当前没有可用的解锁分类!")
                    return
                }
                recycler.visibility = View.VISIBLE
                val adapter = DebLockingAdapter(t.data)
                adapter.onItemClickListener = object : DebLockingAdapter.OnItemClickListener {
                    override fun onItemClick(pos: Int) {
                        val item = t.data[pos]
                        val genre = Genre(
                            item.name,
                            item.id.toLong(),
                            item.genre_id
                        )
                        VideoListActivity.toCategory(this@UnlockMoreActivity, genre)
                    }
                }
                recycler.adapter = adapter
            }

            override fun onFailed(msg: String) {
                if (isFinishing || isDestroyed) return
                loadingView.hideLoading()
                showLockListErrorDialog()
            }
        })
    }



    private fun showLockListErrorDialog() {
        showErrorDialog(
            getString(R.string.error_net),
            getString(R.string.retry),
            {
                hideErrorDialog()
                loadingView.showLoading()
                loadingView.postDelayed({
                    if (isFinishing || isDestroyed) return@postDelayed
                    doHttpUnlockMore()
                }, 1000)
            },
            getString(R.string.cancel),
            {
                hideErrorDialog()
            })
    }

    @Suppress("DEPRECATION")
    @Deprecated("Deprecated in Java")
    override fun onBackPressed() {
        super.onBackPressed()
        val from = intent?.getIntExtra("from", 0)?:0
        if (from == 1) {
            overridePendingTransition(R.anim.anim_enter_right, R.anim.anim_exit_left)
        }
    }
}